﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data;

/// <summary>
/// Summary description for WebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService]

public class WebService : System.Web.Services.WebService {
    public WebService () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod]
    public string HelloWorld() {
        return "Hello World";
    }
    [WebMethod]
    public TaiKhoan checkAcount(string acount, string pass)
    {
        TaiKhoan taiKhoan = DBAccess.layTK(acount,MyFunction.MahoaMatKhau(pass));
        return taiKhoan;
    }
    [WebMethod]
    public int getGianHang(string acount)
    {
        int SoGianHang = DBAccess.getGianHang(acount);
        return SoGianHang;
    }
    [WebMethod]
    public int updateAcount(string tendangnhap,string avarta, string cmnd, string email, string mobile, string nganghangid, string ngaysinh, string noisinhid, string phone, string sotk, string ten, string website)
    {
        DateTime date = DateTime.Parse(ngaysinh);
        TaiKhoan acount = new TaiKhoan();
        acount.TenDangNhap = tendangnhap;
        acount.Avarta = avarta;
        acount.CMND = cmnd;
        acount.Email = email;
        acount.Mobile = mobile;
        acount.NganHangID = int.Parse(nganghangid);
        acount.NgaySinh = date;
        acount.NoiSinhID = int.Parse(noisinhid);
        acount.Phone = phone;
        acount.SoTK = sotk;
        acount.Ten = ten;
        acount.Website = website;

        return DBAccess.updateAcount(acount); 
    }
    [WebMethod]
    public Thu[] getMailInbox(string tendangnhap, int number, int minIDMail)
    {
        return DBAccess.layThu(tendangnhap, number, minIDMail, true);
 
    }
     [WebMethod]
    public Thu[] getMailOutbox(string tendangnhap, int number, int minIDMail)
    {
        return DBAccess.layThu(tendangnhap, number, minIDMail, false);

    }
     [WebMethod]
     public int[] getNumberOfMail(string NguoiNhan, string NguoiGui)
     {
         int[] result = { 0, 0 };
         result = DBAccess.laySoluongThu((NguoiGui.Equals("")?NguoiNhan:NguoiGui), (NguoiGui.Equals("") ? true : false));        
         return result;
             

     }
    [WebMethod]
    public int createMail(string nguoiGui, string nguoinhan, string noidung, string tieude){
        string[] nhanMail = nguoinhan.Split(',');
        int i = 0;
        foreach(string str in nhanMail){       
			if(str!=""){
				Thu m = new Thu();
				m.DaDoc=false;
                m.NgayGui = DateTime.Now;
                m.NguoiGui = nguoiGui;
                m.NguoiNhan = str;
                m.NoiDung = noidung;
                m.TieuDe = tieude;
                m.OpenID = false;
                m.Email="";
                i+=DBAccess.GuiThu(m);
			}
		}
        return i;
    }
    //[WebMethod]
    //public List<GianHang> getListGianHangAcount(string TenDangNhap)
    //{
    //    List<GianHang> gianHangs = DBAccess.getListGianHangAcount(TenDangNhap);
    //    return gianHangs;
    //}
   [WebMethod]
    public List<GianHang> getListGianHangAcount(string tendangnhap)
    {
        return DBAccess.getListGianHangAcount("dinhnam");   
    }
   [WebMethod]
   public List<YKien> geListYKien(int idObject, int numTable, int minID)
   {
       return DBAccess.geListYKien(idObject, numTable, minID);
   }
   [WebMethod]
   public int voteYkien(int idObject, int numTable)
   {
       return DBAccess.voteYkien(idObject, numTable);
   }
   [WebMethod]
   public int addYkien(int idObject, string noiDung,string tenDangNhap,int numTable)
   {
       return DBAccess.addYkien(idObject, noiDung, tenDangNhap, "", numTable);
   }
   [WebMethod]
   public int createOrder(string tenDangNhap, string listGianHangID,string listSanPhamID, string listMasoSP, string listSoLuong)
   {
       String[] listGianHangIDs = (listGianHangID+",").Split(',');
       String[] listSanPhamIDs = (listSanPhamID+",").Split(',');
       String[] listMasoSPs = (listMasoSP+",").Split(',');
       String[] listSoLuongs = (listSoLuong+",").Split(',');
       int[] soluongs = new int[listSoLuongs.Length-1];
       for (int i = 0; i < listSoLuong.Length; i++)
       {
           soluongs[i] = int.Parse(listSoLuongs[i]);
       }

       DBAccess.createOrther(tenDangNhap, listGianHangIDs, listSanPhamIDs, listMasoSPs, soluongs);
       return 1;
   }
   [WebMethod]
   public List<GianHang> searchSanPham(string strSearch, int typeSearch)
   {   
        return DBAccess.searchSanPham(typeSearch, strSearch);
      
   }
   [WebMethod]
   public List<SanPham> getListSanPhamOfDanhMuc(int iDDanhMuc)
   {
       return DBAccess.getListSanPhamOfDanhMuc(iDDanhMuc);

   }
   [WebMethod]
   public string test(int type, string strSearch)
   {
       string sqlQuery = "";
       switch (type)
       {
           case 0:
               sqlQuery = "Select * from View_GianHang where IDGIanHang IN("
               + "Select distinct GianHangID from GianHangSanPham where GianHangSanPham.SanPhamID IN ("
               + "select IDSanPham from View_SanPham where Lower(LTRIM(RTRIM(dbo.fuChuyenCoDauThanhKhongDau(View_SanPham.TenSanPham))))LIKE '%" + MyFunction.LocDau(strSearch).ToLower() + "%' ))";
               break;
           case 1:
              int[] idDanhMuc = DBAccess.getIDDanhMuc(strSearch);
              DataTable tbDanhMuc = DBAccess.loadDanhMuc();
              for(int i = 0; i<idDanhMuc.Length;i++)
               {
                   string strChild =","+idDanhMuc[i]+ DBAccess.findChildInDanhMuc(idDanhMuc[i], tbDanhMuc);
                   string strListIDGianHang = DBAccess.getIDGianHangOfDanhMucID(strChild.Substring(1));
                   sqlQuery += " Select distinct * from View_GianHang where IDGIanHang IN(" + ((strListIDGianHang.Length > 0) ? strListIDGianHang.Substring(1) : strListIDGianHang) + ");";                  
               }
               break;
           case 2:
               sqlQuery = "Select * from View_GianHang where Lower(LTRIM(RTRIM(dbo.fuChuyenCoDauThanhKhongDau(View_GianHang.TenGianHang))))LIKE '%" + MyFunction.LocDau(strSearch).ToLower() + "%' ";
               break;
           case 3:
               sqlQuery = "Select * from View_GianHang where View_GianHang.TenDangNhap LIKE '%" + MyFunction.LocDau(strSearch).ToLower() + "%' ";
               break;
       }
       return sqlQuery;
   }
   [WebMethod]
   public int changePass(string tendangnhap, string matkhaucu, string matkhaumoi)
   {
       return DBAccess.changePass(tendangnhap, matkhaucu, matkhaumoi);
   }
}
